package com.systop.workview.controller;
import com.systop.common.util.ResponseResult;
import com.systop.workview.service.SuperviseClientService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.UUID;

@RestController
@RequestMapping("/workview")
public class SuperviseController {

    //编译器报错，无视。 因为这个Bean是在程序启动的时候注入的，编译器感知不到，所以报错。
    @Autowired
    SuperviseClientService SuperviseService;

    //login
    @PostMapping (value = "/superviseLogin2")
    public ResponseResult superviseLogin(@RequestParam(value="tno") String tno, @RequestParam (value="password") String password) {
        return SuperviseService.superviseLogin1(tno, password);
    }
    //查全部
    @PostMapping (value = "/teacherSupList1")
    public ResponseResult teacherSupList() {
        return SuperviseService.superviselist();
    }
    //根据tno查询
    @PostMapping (value = "/updataSupervice11")
    public ResponseResult updataSupervice1(@RequestParam(value = "tno") String tno) {
        return SuperviseService.updataSupervice1(tno);
    }
    //页面的读取文件方法
    @RequestMapping("/imgshows")
    public void picShow(HttpServletRequest request, HttpServletResponse response, String picName) throws IOException {
        picName=new String(picName.getBytes("ISO8859-1"),"UTF-8"); //防止中文乱码
        String filePath = "D:\\upload/upload/";
        String imagePath =filePath+picName;
        response.reset();
        //判断文件是否存在
        File file = new File(imagePath);
        if (!file.exists()) {
            imagePath =filePath+picName;
        }
        // 得到输出流
        OutputStream output = response.getOutputStream();
        FileInputStream is = new FileInputStream(new File(imagePath));
        int i = is.available(); // 得到文件大小
        byte data[] = new byte[i];
        is.read(data); // 读数据
        is.close();
        response.setContentType("image/*"); // 设置返回的文件类型
        OutputStream toClient = response.getOutputStream(); // 得到向客户端输出二进制数据的对象
        toClient.write(data); // 输出数据
        toClient.close();
        output.close();
    }

    //添加
    @PostMapping (value = "/superviseadd1")
    public ResponseResult superviseadd1(@RequestParam(value = "phone_url",required = false)  MultipartFile phone_url1, @RequestParam(value = "tno")String tno,@RequestParam(value = "name")String name, @RequestParam(value = "college_name")String college_name, @RequestParam(value = "password")String password,@RequestParam(value = "professional")String professional,@RequestParam(value = "sex")String sex,@RequestParam(value = "type")String type,@RequestParam(value = "remark")String remark) throws IOException {
        System.out.println("-----------------------------"+phone_url1+"------------------------------------");

        String phone_url="";
        //文件的原始名称
        if (phone_url!=null) {
            String originalFilename = phone_url1.getOriginalFilename();
            //查找项目中是否有upload路径
            String path = "D:\\upload/upload";
            //新建file文件
            File filepath = new File(path);
            if (!filepath.exists()) {
                filepath.mkdirs();
            }
            //生成新的名字
            String newFileName = UUID.randomUUID() + "_" + originalFilename;
//        try {
            //使用MultipartFile类transferTo方法上传参数是个文件filepath+newFileName==/upload/xxx_1.txt
            File targetFile1 = new File(filepath, newFileName);
            phone_url1.transferTo(targetFile1);
            phone_url = newFileName;
            System.out.println("-----------------------------" + phone_url+ "------------------------------------");
        }

        return SuperviseService.superviseadd(tno,name,college_name,password,professional,sex,phone_url,type,remark);
//        } catch (Exception e) {
//					// TODO 自动生成的 catch 块
//					e.printStackTrace();
//					System.out.println("失败");
//        }
    }
    //修改
    @PostMapping (value = "/updataSupervice1")
    public ResponseResult updataSupervice(@RequestParam(value = "tno")String tno,@RequestParam(value = "name")String name, @RequestParam(value = "college_name")String college_name, @RequestParam(value = "password")String password,@RequestParam(value = "professional")String professional,@RequestParam(value = "sex")String sex,@RequestParam(value = "phone_url",required = false)  MultipartFile phone_url1,@RequestParam(value = "type")String type,@RequestParam(value = "remark")String remark) throws IOException {
        System.out.println("-----------------------------" + phone_url1 + "------------------------------------");
        String phone_url="";
        //文件的原始名称
        if (phone_url1!=null) {
            String originalFilename = phone_url1.getOriginalFilename();
            //查找项目中是否有upload路径
            String path = "D:\\upload/upload";
            //新建file文件
            File filepath = new File(path);
            if (!filepath.exists()) {
                filepath.mkdirs();
            }
            //生成新的名字
            String newFileName = UUID.randomUUID() + "_" + originalFilename;
//        try {
            //使用MultipartFile类transferTo方法上传参数是个文件filepath+newFileName==/upload/xxx_1.txt
            File targetFile1 = new File(filepath, newFileName);
            phone_url1.transferTo(targetFile1);
            phone_url = newFileName;
            System.out.println("-----------------------------" + phone_url + "------------------------------------");
        }
        return SuperviseService.updataSupervice(tno,name,college_name,password,professional,sex,phone_url,type,remark);
    }
    //删除
    @PostMapping (value = "/supervisedel1")
    public ResponseResult deletesupTeacher(@RequestParam(value = "tno") String tno) {
        return SuperviseService.supervisedel(tno);
    }

    //---------------------------课程-----course_library-------------

    //查看全部
    @PostMapping (value = "/sourse_libraryList")
    public ResponseResult sourse_libraryList() {
        return SuperviseService.sourse_libraryList();
    }

    //添加
    @PostMapping (value = "/insertCourse_library")
    public ResponseResult insertCourse_library(@RequestParam(value = "course_id") String course_id, @RequestParam(value = "course_name") String course_name, @RequestParam(value = "college_name") String college_name, @RequestParam(value = "performance_rule_id") String performance_rule_id, @RequestParam(value = "class_hour") String class_hour, @RequestParam(value = "credit") String credit, @RequestParam(value = "course_group_name") String course_group_name, @RequestParam(value = "introduction") String introduction, @RequestParam(value = "remark") String remark) {
        return SuperviseService.insertCourse_library(course_id,course_name,college_name,performance_rule_id,class_hour,credit,course_group_name,introduction,remark);
    }

    //修改
    @PostMapping (value = "/updataCourse_library")
    public ResponseResult updataCourse_library(@RequestParam(value = "course_id") String course_id, @RequestParam(value = "course_name") String course_name, @RequestParam(value = "college_name") String college_name, @RequestParam(value = "performance_rule_id") String performance_rule_id, @RequestParam(value = "class_hour") String class_hour, @RequestParam(value = "credit") String credit, @RequestParam(value = "course_group_name") String course_group_name, @RequestParam(value = "introduction") String introduction, @RequestParam(value = "remark") String remark) {
        return SuperviseService.updataCourse_library(course_id,course_name,college_name,performance_rule_id,class_hour,credit,course_group_name,introduction,remark);
    }

    //通过id查
    @PostMapping (value = "/course_libraryIdlist")
    public ResponseResult course_libraryIdlist(@RequestParam(value = "course_id") String course_id) {
        return SuperviseService.course_libraryIdlist(course_id);
    }

    //删除
    @PostMapping (value = "/deleteCourse_library")
    public ResponseResult deleteCourse_library(@RequestParam(value = "course_id") String course_id) {
        return SuperviseService.deleteCourse_library(course_id);
    }
//-------------------------------------排课------------------------
   //查看全部
   @PostMapping (value = "/elcourall")
   public ResponseResult elcourall() {
    return SuperviseService.elcourall();
}

    //修改
    @PostMapping (value = "/elcourupdate")
    public ResponseResult elcourupdate(@RequestParam(value = "elective_course_id") String elective_course_id,
                                       @RequestParam(value = "course_id") String course_id,
                                       @RequestParam(value = "tno") String tno,
                                       @RequestParam(value = "college_name") String college_name,
                                       @RequestParam(value = "restricted_grade") String restricted_grade,
                                       @RequestParam(value = "time") String time,
                                       @RequestParam(value = "know") String know,
                                       @RequestParam(value = "place") String place,
                                       @RequestParam(value = "max_number") String max_number,
                                       @RequestParam(value = "min_number") String min_number,
                                       @RequestParam(value = "current_number") String current_number,
                                       @RequestParam(value = "remark") String remark) {
        return SuperviseService.elcourupdate(elective_course_id,course_id,tno,college_name,restricted_grade,time,know,place,max_number,min_number,current_number,remark);
    }
    //添加
    @PostMapping (value = "/elcouradd")
    public ResponseResult elcouradd(@RequestParam(value = "course_id") String course_id,@RequestParam(value = "tno") String tno,@RequestParam(value = "college_name") String college_name, @RequestParam(value = "restricted_grade") String restricted_grade, @RequestParam(value = "time") String time, @RequestParam(value = "know") String know, @RequestParam(value = "place") String place, @RequestParam(value = "max_number") String max_number, @RequestParam(value = "min_number") String min_number, @RequestParam(value = "current_number") String current_number,@RequestParam(value = "remark") String remark) {
        System.out.println(course_id+"-"+tno+"-"+college_name+"-"+restricted_grade+"-"+time+"-"+know+"-"+place+"-"+max_number+"-"+min_number+"-"+current_number+"-"+remark);
        return SuperviseService.eelcouradd(course_id,tno,college_name,restricted_grade,time,know,place,max_number,min_number,current_number,remark);
    }
    //通过id查实体类
    @PostMapping (value = "/elcourid")
    public ResponseResult elcourid(@RequestParam(value = "elective_course_id") String elective_course_id) {
        return SuperviseService.elcourid(elective_course_id);
    }
    //通过id查list
    @PostMapping (value = "/elcourid1")
    public ResponseResult elcourid1(@RequestParam(value = "elective_course_id") String elective_course_id) {
        return SuperviseService.elcourid1(elective_course_id);
    }
    //通过id删除
    @PostMapping (value = "/elcourdel")
    public ResponseResult elcourdel(@RequestParam(value = "elective_course_id") String elective_course_id) {
        return SuperviseService.elcourdel(elective_course_id);
    }
    //详情查看课程信息elcourtno
    //通过id删除
    @PostMapping (value = "/elcourxiangqing")
    public ResponseResult elcourxiangqing(@RequestParam(value = "elective_course_id") String elective_course_id) {
        return SuperviseService.elcourxiangqing(elective_course_id);
    }
    //详情查看tno课程信息
    @PostMapping (value = "/elcourtno")
    public ResponseResult elcourtno(@RequestParam(value = "tno") String tno) {
        return SuperviseService.elcourtno(tno);
    }
//-------------------------------成绩-----------
    //添加
    @RequestMapping(value = "/performanceadd",method = RequestMethod.POST)
    public ResponseResult performanceadd(
                              @RequestParam(value = "sno") String sno,
                              @RequestParam(value = "elective_course_id") String elective_course_id,
                              @RequestParam(value = "tno") String tno,
                              @RequestParam(value = "performance_rule_id") String performance_rule_id,
                              @RequestParam(value = "peacetime_performance") String peacetime_performance,
                              @RequestParam(value = "mid_term_performance") String mid_term_performance,
                              @RequestParam(value = "final_performance") String final_performance,
                              @RequestParam(value = "skill_assessment") String skill_assessment,
                              @RequestParam(value = "total_score") String total_score,
                              @RequestParam(value = "pass_flag") String pass_flag,
                              @RequestParam(value = "allow_flag") String allow_flag,
                              @RequestParam(value = "remark") String remark){
        System.out.println(sno+"-"+elective_course_id+"-"+tno+"-"+performance_rule_id+"-"+peacetime_performance+"-"+mid_term_performance+"-"+final_performance+"-"+skill_assessment+"-"+total_score+"-"+pass_flag+"-"+allow_flag+"-"+remark);
        return SuperviseService.performanceadd(sno,elective_course_id,tno,performance_rule_id,peacetime_performance,mid_term_performance,final_performance,skill_assessment,total_score,pass_flag,allow_flag,remark);
    }
    //2通过tno查list
    @RequestMapping(value = "/performancetno",method = RequestMethod.POST)
    public ResponseResult performancetno(@RequestParam(value = "tno") String tno){
        return  SuperviseService.performancetno(tno);
    }
    //3通过成绩id 查list
    @RequestMapping(value = "/performance_id_list",method = RequestMethod.POST)
    public ResponseResult performance_id_list(@RequestParam(value = "performance_id") String performance_id){
        return SuperviseService.performance_id_list(performance_id);
    }
    //根据id修改
    @RequestMapping(value = "/performanceUp",method = RequestMethod.POST)
    public ResponseResult performanceUp(@RequestParam(value = "performance_id") String performance_id,
                                       @RequestParam(value = "sno") String sno,
                                       @RequestParam(value = "elective_course_id") String elective_course_id,
                                       @RequestParam(value = "tno") String tno,
                                       @RequestParam(value = "performance_rule_id") String performance_rule_id,
                                       @RequestParam(value = "peacetime_performance") String peacetime_performance,
                                       @RequestParam(value = "mid_term_performance") String mid_term_performance,
                                       @RequestParam(value = "final_performance") String final_performance,
                                       @RequestParam(value = "skill_assessment") String skill_assessment,
                                       @RequestParam(value = "total_score") String total_score,
                                       @RequestParam(value = "pass_flag") String pass_flag,
                                       @RequestParam(value = "allow_flag") String allow_flag,
                                       @RequestParam(value = "remark") String remark){
        return  SuperviseService.performanceUp(performance_id,sno,elective_course_id,tno,performance_rule_id,peacetime_performance,mid_term_performance,final_performance,skill_assessment,total_score,pass_flag,allow_flag,remark);
    }
    //3-1通过成绩id 查 实体
    @RequestMapping(value = "/performance_id_p",method = RequestMethod.POST)
    public ResponseResult performance_id_p(@RequestParam(value = "performance_id") String performance_id){
        return SuperviseService.performance_id_p(performance_id);
    }
    //3-2通过排课id查list
    @RequestMapping(value = "/paikeId_tno",method = RequestMethod.POST)
    public  ResponseResult paikeId_tno(@RequestParam(value = "elective_course_id") String elective_course_id){
        return SuperviseService.paikeId_tno(elective_course_id);
    }
    //5通过sno和排课id和状态sno elective_course_id allow_flag
    @RequestMapping(value = "/sno_pkid_allow", method = RequestMethod.POST)
    public  ResponseResult sno_pkid_allow(@RequestParam(value = "sno") String sno,@RequestParam(value = "elective_course_id") String elective_course_id,@RequestParam(value = "allow_flag") String allow_flag){
        return SuperviseService.sno_pkid_allow(sno,elective_course_id,allow_flag);
    }
    //6通过tno修改发布信息
    @RequestMapping(value = "/allow_flagfabu", method = RequestMethod.POST)
    public ResponseResult allow_flagfabu(@RequestParam(value = "tno") String tno,@RequestParam(value = "allow_flag") String allow_flag){
        return SuperviseService.allow_flagfabu(tno,allow_flag);
    }
    //查询全部 performanceall
    @RequestMapping(value = "/performanceall", method = RequestMethod.POST)
    public ResponseResult performanceall(){
        return SuperviseService.performanceall();
    }
    // 6通过id删除 performancedel
    @RequestMapping(value = "/performancedel", method = RequestMethod.POST)
    public  ResponseResult performancedel(@RequestParam(value = "performance_id") String performance_id){
        return SuperviseService.performancedel(performance_id);
    }

}
